'''
Created on Aug 6, 2013

@author: Yuanpeng_Li

1.7 Write an algorithm such that if an element in an MxN matrix is 0,
its entire row and column is set to 0.
'''

def set_zero(m):
    h = len(m)
    w = len(m[0])
    b = [[1]*w for y in range(0, h)]
    for i in range(0, h):
        for j in range(0, w):
            if m[i][j] == 0:
                for k in range(0, w):
                    b[i][k] = 0
                for k in range(0, h):
                    b[k][j] = 0
    for i in range(0, h):
        for j in range(0, w):
            if b[i][j] == 0:
                m[i][j] = 0
    return m
    
def print_matrix(m):
    for i in m:
        print (i)
        
if __name__ == '__main__':
    m = [[1, 0, 1], [1, 0, 1], [1, 1, 1]]
    print_matrix(m)
    m = set_zero(m)
    print_matrix(m)